package com.whut.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whut.mapper.*;
import com.whut.model.Contractinfo;
import com.whut.model.Contracttaskaccept;
import com.whut.model.ContractTaskProject;
import com.whut.model.Equipinfo;
import com.whut.service.InfoManageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service("/infoManageService")
public class InfoManageServiceImpl implements InfoManageService {

    @Resource
    private ContracttaskprojectMapper contracttaskprojectMapper;
    @Resource
    private ContractinfoMapper contractinfoMapper;
    @Resource
    private EquipinfoMapper equipinfoMapper;
    @Resource
    private ContracttaskacceptMapper contracttaskacceptMapper;
    @Resource
    private ContractchecktaskMapper contractchecktaskMapper;

    @Override
    public Object getProjectInfoByPage(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<ContractTaskProject> list = contracttaskprojectMapper.getAllProject();
        PageInfo result = new PageInfo(list);
        return result;
    }

    @Override
    public Object getProjectInfoBySearchParams(String searchParams,int pageNum,int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<ContractTaskProject> list = contracttaskprojectMapper.getProjectInfoBySearchParams(searchParams);
        PageInfo result = new PageInfo(list);
        return result;
    }

    @Override
    public Object getProjectInfoBySearchParams2(String searchParams){
        List<ContractTaskProject> list = contracttaskprojectMapper.getProjectInfoBySearchParams(searchParams);
        PageInfo result = new PageInfo(list);
        return result;
    }

    @Override
    public String deleteProjectInfoById(int projectId){
        try{
            contracttaskprojectMapper.deleteByPrimaryKey(projectId);
            return "success";
        } catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @Override
    public Object getContractClient(){
        List<Contractinfo> list = contractinfoMapper.getContractClient();
        return list;
    }

    @Override
    public Object getClientByContractId(int contractId){
        String client = contractinfoMapper.getClientByContractId(contractId);
        return client;
    }

    @Override
    public String addProjectInfo(ContractTaskProject info){
        try{
            SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
            Date date = new Date(System.currentTimeMillis());
            info.setNewCreateTime(formatter.format(date));
            info.setLastEditTime(formatter.format(date));
            contracttaskprojectMapper.insertInfo(info);
            return "success";
        }catch(Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @Override
    public String updateProjectInfo(ContractTaskProject info){
        try{
            SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
            Date date = new Date(System.currentTimeMillis());
            info.setLastEditTime(formatter.format(date));
            contracttaskprojectMapper.updateByPrimaryKeySelective(info);
            return "success";
        }catch(Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @Override
    public String importExcel(String filePath){
        Workbook checktable = openExcel(filePath);
        int sheetCount=checktable.getNumberOfSheets();
        for(int i = 0;i < sheetCount;i++){
            Sheet sheet = checktable.getSheetAt(i);
            CellReference cellReference = new CellReference("A4");
            boolean flag = false;
            for (int j = cellReference.getRow(); j <= sheet.getLastRowNum();){
                Row r = sheet.getRow(j);
                if(r == null){
                    sheet.shiftRows(j+1, sheet.getLastRowNum(),-1);
                    continue;
                }
                flag = false;
                for(Cell c:r){
                    if(c.getCellType() != Cell.CELL_TYPE_BLANK){
                        flag = true;
                        break;
                    }
                }
                if(flag){
                    j++;
                    continue;
                }
                else{
                    if(j == sheet.getLastRowNum())
                        sheet.removeRow(r);
                    else
                        sheet.shiftRows(j+1, sheet.getLastRowNum(),-1);
                }
            }
            int rowCount=sheet.getLastRowNum();
            String temp_item = null,temp_pro = null;
            for(int rowindex=2;rowindex<=rowCount;rowindex++){
                ContractTaskProject contracttaskproject = new ContractTaskProject();
                if(sheet.getRow(0).getPhysicalNumberOfCells()>=2){
                    Row row=sheet.getRow(rowindex);
                    int cellCount=row.getPhysicalNumberOfCells();
                    for(int cellindex=1;cellindex < cellCount;cellindex++){
                        Cell cell=row.getCell(cellindex);
                        if(cellindex==0) {
                            continue;
                        }else {
                            String temp = null;
                            if(cellindex<17)
                                temp = cell.getRichStringCellValue().getString().trim();
                            else if(cellindex<18)
                                temp = String.valueOf(cell.getNumericCellValue());
                            /*else if(cellindex<23)
                                temp = cell.getRichStringCellValue().getString().trim();
                            else if(cellindex<24)
                                temp = String.valueOf(cell.getNumericCellValue());*/
                            else
                                temp = cell.getRichStringCellValue().getString().trim();
                            switch (cellindex) {
                                case 1: {
                                    int contractId = contractinfoMapper.getContractIdByClient(temp);
                                    //System.out.println("haha"+contractinfoMapper.getContractIdByClient(temp));
                                    contracttaskproject.setContractId(contractId);
                                    contracttaskproject.setTaskId(0);
                                    break;
                                }
                                case 2: {
                                    contracttaskproject.setCheckContentDes(temp);
                                    break;
                                }
                                case 3: {
                                    contracttaskproject.setProjectName(temp);
                                    break;
                                }
                                case 4: {
                                    contracttaskproject.setProjectCategory(temp);
                                    break;
                                }
                                case 5: {
                                    contracttaskproject.setProjectType(temp);
                                    break;
                                }
                                case 6: {
                                    contracttaskproject.setSgUnit(temp);
                                    break;
                                }
                                case 7: {
                                    contracttaskproject.setManager(temp);
                                    break;
                                }
                                case 8: {
                                    contracttaskproject.setJsUnit(temp);
                                    break;
                                }
                                case 9: {
                                    contracttaskproject.setJsUniter(temp);
                                    break;
                                }
                                case 10: {
                                    contracttaskproject.setJlUnit(temp);
                                    break;
                                }
                                case 11: {
                                    contracttaskproject.setJlUniter(temp);
                                    break;
                                }
                                case 12: {
                                    contracttaskproject.setArea(temp);
                                    break;
                                }
                                case 13: {
                                    contracttaskproject.setCheckTime(temp);
                                    break;
                                }
                                case 14: {
                                    contracttaskproject.setCheckSection(temp);
                                    break;
                                }
                                case 15: {
                                    contracttaskproject.setProgress(temp);
                                    break;
                                }
                                case 16: {
                                    contracttaskproject.setFinishState(temp);
                                    break;
                                }
                                case 17: {
                                    contracttaskproject.setProjectPrice(temp);
                                    break;
                                }
                                case 18: {
                                   contracttaskproject.setProjectDescription(temp);
                                    break;
                                }
                                case 19: {
                                    contracttaskproject.setDangerItem(temp);
                                    break;
                                }
                                case 20: {
                                    contracttaskproject.setRemark(temp);
                                    break;
                                }
                            }
                        }
                    }
                }
                SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
                Date date = new Date(System.currentTimeMillis());
                contracttaskproject.setLastEditTime(formatter.format(date));
                contracttaskprojectMapper.insertInfo(contracttaskproject);
            }
        }
        return "success";
    }

    private Workbook openExcel(String ExcelName){
        InputStream inp = null;
        try{
            inp = new FileInputStream(ExcelName);
        }catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        Workbook wookbook = null;
        try {
            wookbook = WorkbookFactory.create(inp);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return wookbook;
    }

    public String importExcel2(String filePath){
        Workbook checktable = openExcel(filePath);
        int sheetCount=checktable.getNumberOfSheets();
        for(int i = 0;i < sheetCount;i++){
            Sheet sheet = checktable.getSheetAt(i);
            CellReference cellReference = new CellReference("A4");
            boolean flag = false;
            for (int j = cellReference.getRow(); j <= sheet.getLastRowNum();){
                Row r = sheet.getRow(j);
                if(r == null){
                    sheet.shiftRows(j+1, sheet.getLastRowNum(),-1);
                    continue;
                }
                flag = false;
                for(Cell c:r){
                    if(c.getCellType() != Cell.CELL_TYPE_BLANK){
                        flag = true;
                        break;
                    }
                }
                if(flag){
                    j++;
                    continue;
                }
                else{
                    if(j == sheet.getLastRowNum())
                        sheet.removeRow(r);
                    else
                        sheet.shiftRows(j+1, sheet.getLastRowNum(),-1);
                }
            }
            int rowCount=sheet.getLastRowNum();
            String temp_item = null,temp_pro = null;
            for(int rowindex=2;rowindex<=rowCount;rowindex++){
                Equipinfo equipinfo = new Equipinfo();
                if(sheet.getRow(0).getPhysicalNumberOfCells()>=2){
                    Row row=sheet.getRow(rowindex);
                    int cellCount=row.getPhysicalNumberOfCells();
                    for(int cellindex=1;cellindex < cellCount;cellindex++){
                        Cell cell=row.getCell(cellindex);
                        if(cellindex==0) {
                            continue;
                        }else {
                            String temp = null;
                            if(cellindex<8)
                                temp = cell.getRichStringCellValue().getString().trim();
                            else
                                temp = String.valueOf(cell.getNumericCellValue());
                            switch (cellindex) {
                                case 1: {
                                    int projectId = contracttaskprojectMapper.getProjectIdByProjectName(temp);
                                    equipinfo.setProjectId(projectId);
                                    break;
                                }
                                case 2: {
                                    equipinfo.setEquipType(temp);
                                    break;
                                }
                                case 3: {
                                    equipinfo.setEquipNum(temp);
                                    break;
                                }
                                case 4: {
                                    equipinfo.setEquipLeaseUnit(temp);
                                    break;
                                }
                                case 5: {
                                    equipinfo.setEquipUseUnit(temp);
                                    break;
                                }
                                case 6: {
                                    equipinfo.setDemolitionUnit(temp);
                                    break;
                                }
                                case 7: {
                                    equipinfo.setTestUnit(temp);
                                    break;
                                }
                                case 8: {
                                    equipinfo.setEquipQua(temp);
                                    break;
                                }

                            }
                        }
                    }
                }
                equipinfoMapper.insertInfo(equipinfo);
            }
        }
        return "success";
    }

    @Override
    public Object getEquipInfo(int projectId){
        List<Equipinfo> list = equipinfoMapper.getAllByProjectId(projectId);
        return list;
    }

    /*wyh*/
    /*在表constractTaskProject中利用合同id获取项目信息返回前端*/
    public Object getProjectInfobyContractId(int contractId){
        List<ContractTaskProject> list =contracttaskprojectMapper.getAllProjectInfo(contractId);
        return list;
    }

    @Override
    public String finishZhouqi(int taskId, String finishTime) {
        try {
            Contracttaskaccept contracttaskaccept=new Contracttaskaccept();
            SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
            Date date = new Date(System.currentTimeMillis());
            contracttaskaccept.setLastEditTime(formatter.format(date));
            contracttaskaccept.setTaskId(taskId);
            contracttaskaccept.setIsFinished("Y");
            contracttaskaccept.setFinishRound(finishTime);
            contracttaskacceptMapper.updateByPrimaryKeySelective(contracttaskaccept);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @Override
    public Object saveEquip(Equipinfo equipinfo){
        return equipinfoMapper.updateByPrimaryKey(equipinfo);
    }

    @Override
    public boolean editContractInfo(Contractinfo ci){
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        ci.setLastEditTime(formatter.format(date));
        return contractinfoMapper.editContractInfo(ci);
    }

    @Override
    public Object getDataByContractId(Integer contractId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ContractTaskProject> list = contracttaskprojectMapper.getDataByContractId(contractId);
        PageInfo result = new PageInfo(list);
        return result;
    }

    @Override
    public Object getJsUnitByContractId(List<Integer> list) {
        //判断是否全选
        int index = list.indexOf(-1);
        if(list.size()==0||index!=-1){
            list=null;
        }
        List<String> result = contracttaskprojectMapper.getJsUnitByContractId(list);
        return result;
    }

    @Override
    public Object getGsUnitByContractId(List<Integer> list) {
        //判断是否全选
        int index = list.indexOf(-1);
        if(list.size()==0||index!=-1){
            list=null;
        }
        List<String> result = contracttaskprojectMapper.getGsUnitByContractId(list);
        return result;
    }

    @Override
    public Object getJlUnitByContractId(List<Integer> list) {
        //判断是否全选
        int index = list.indexOf(-1);
        if(list.size()==0||index!=-1){
            list=null;
        }
        List<String> result = contracttaskprojectMapper.getJlUnitByContractId(list);
        return result;
    }

    @Override
    public Object getTaskByContractIds(List<Integer> list) {
        //判断是否全选
        int index = list.indexOf(-1);
        if(list.size()==0||index!=-1){
            list=null;
        }
        List<Contracttaskaccept> result = contracttaskacceptMapper.getTaskByContractIds(list);
        return result;
    }

    @Override
    public Object getProjectBYTasKIdAndContractId(List<Integer> contractList, List<Integer> taskList) {
        //判断是否全选
        int index = contractList.indexOf(-1);
        if(contractList.size()==0||index!=-1){
            contractList=null;
        }
        if(taskList.size()==0||taskList.indexOf(-1)!=-1){
            taskList=null;
        }
        List<Contracttaskaccept> result = contracttaskprojectMapper.getProjectBYTasKIdAndContractId(contractList,taskList);
        return result;
    }

    @Override
    public Object getProjectsByParams(List<Integer> contracts, List<String> jsUnits, List<String> sgUnits, List<String> jlUnits, List<Integer> tasks, List<Integer> projects, int pageNum) {
        //全选或者为空
        if(contracts.size()==0||contracts.indexOf(-1)!=-1){
            contracts=null;
        }
        if(jsUnits.size()==0||jsUnits.indexOf("全选")!=-1){
            jsUnits=null;
        }
        if(sgUnits.size()==0||sgUnits.indexOf("全选")!=-1){
            sgUnits=null;
        }
        if(jlUnits.size()==0||jlUnits.indexOf("全选")!=-1){
            jlUnits=null;
        }
        if(tasks.size()==0||tasks.indexOf(-1)!=-1){
            tasks=null;
        }
        if(projects.size()==0||projects.indexOf(-1)!=-1){
            projects=null;
        }
        PageHelper.startPage(pageNum,6);
        List<Map> result = contractchecktaskMapper.getProjectsByParams(contracts,jsUnits,sgUnits,jlUnits,tasks,projects);
        PageInfo pageInfo = new PageInfo(result);
        return pageInfo;
    }
}
